import { h, ref, unref, reactive } from 'vue'
import { CrudSchema } from '@/hooks/web/useCrudSchemas'
import { findDictTypeList} from '@/api/common'
import tabTag from '../components/tabTag.vue'
import tabButton from '../components/tabButton.vue'
import { dataToLabel,dataToSelect} from '@/utils'
import {dictType} from '@/utils/dict'
let router:any = null
const mountId:any = null
/**
 *  获取路由对象
 */
export function getRouterInstance(r) {
  router = r
}

let DT:any = null

// 获取data对象
export function getDataInstance(data) {
  DT = data
}

const searchParams = ref({})

// 设备状态
const droneOnLineStatus = ref<dictType[]>([]) 
const droneOnLineStatusToLabel = ref({}) 
// 认证状态
const droneAuthStatus = ref<dictType[]>([]) 
const droneAuthStatusToLabel =  ref({}) 
// 品牌
const droneBrand = ref<dictType[]>([]) 
const droneBrandToLabel = ref({}) 
// 无人机型号
const droneModelCode = ref<dictType[]>([]) 
const droneModelCodeToLabel = ref({}) 
// 无人机状态
const droneStatus = ref<dictType[]>([]) 

// 跳转详情 info 详情  edit 编辑
const linkTo = (row: Object, type: string) => {
  router.push({ path: '/resource-manage/drone_info', query: { type,id:row?.id } })
}


//  无人机类型
export const setDroneType = async function () {
  const {data=[]} = await  findDictTypeList({prefix:'drone'}) as any
  const drone_on_line_status = data.find(item=> item.dictType==='drone_on_line_status').dateList ||[]
  const drone_auth_status = data.find(item=> item.dictType==='drone_auth_status').dateList ||[]
  const drone_brand = data.find(item=> item.dictType==='drone_brand').dateList ||[]
  const drone_model_code = data.find(item=> item.dictType==='drone_model_code').dateList ||[]
  const drone_status = data.find(item=> item.dictType==='drone_status').dateList ||[]
  // 设备状态
  droneOnLineStatus.value = dataToSelect(drone_on_line_status, { label: 'dictLabel', value: 'dictValue' }) 
  droneOnLineStatusToLabel.value = dataToLabel(drone_on_line_status, 'dictValue','dictLabel') 
  // 认证状态
  droneAuthStatus.value = dataToSelect(drone_auth_status, { label: 'dictLabel', value: 'dictValue' }) 
  droneAuthStatusToLabel.value = dataToLabel(drone_auth_status, 'dictValue','dictLabel') 
  // 品牌
  droneBrand.value  = dataToSelect(drone_brand, { label: 'dictLabel', value: 'dictValue' }) 
  droneBrandToLabel.value  =dataToLabel(drone_brand,'dictValue','dictLabel') 
  // 无人机型号
  droneModelCode.value = dataToSelect(drone_model_code, { label: 'dictLabel', value: 'dictValue' }) 
  droneModelCodeToLabel.value = dataToLabel(drone_model_code,'dictValue','dictLabel') 
  // 无人机状态
  droneStatus.value  = dataToSelect(drone_status, { label: 'dictLabel', value: 'dictValue' }) 
}


// 认证状态
const mountTypeToLabel = ref({}) 
//  挂载类型
export const setMountType = async function () {
  const {data=[]} = await  findDictTypeList({prefix:'mount'}) as any
  const mount_type = data.find(item=> item.dictType==='mount_type').dateList ||[]
  mountTypeToLabel.value = dataToLabel(mount_type,'dictValue','dictLabel') 
}


// 列表页对象
export default reactive<CrudSchema[]>([
  {
    field: 'index',
    label: '序号',
    type: 'index',
    search: {
      hidden: true
    },
  },
  {
    field: 'code',
    label: '无人机编号',
    table: {
      slots: {
        default: (data: any) => {
          return data.row.code
        }
      }
    },
    search: {
      hidden: true
    },
  },
  {
    field: 'name',
    label: '无人机名称',
    table: {
      slots: {
        default: (data: any) => {
          return data.row.name
        }
      }
    },
  },
  {
    field: 'brand',
    label: '品牌',
    table: {
      slots: {
        default: (data: any) => {
          return  droneBrandToLabel.value[data.row.brand]
        }
      }
    },
    search:{
      component: 'Select',
      componentProps: {
        options:droneBrand
      },
    }
  },
  {
    field: 'modelCode',
    label: '无人机型号',
    table: {
      slots: {
        default: (data: any) => {
          return droneModelCodeToLabel.value[data.row.modelCode] 
        }
      }
    },
    search: {
      hidden: true
    },
  },
  {
    field: 'status',
    width: '120px',
    label: '可用状态',
    table: {
      slots: {
        default: (data: any) => {
          const status = data.row.status
          return h(tabTag, {
            type: status
          })
        }
      }
    }, 
    search:{
      component: 'Select',
      componentProps: {
        options:droneStatus
      },
    }
  },
  {
    field: 'onLineStatus',
    width: '120px',
    label: '设备状态',
    table: {
      slots: {
        default: (data: any) => {
          return   droneOnLineStatusToLabel.value[data.row.onLineStatus] 
        }
      }
    },
    search:{
      component: 'Select',
      componentProps: {
        options:droneOnLineStatus
      },
    }
  },
  {
    field: 'mounts',
    label: '关联挂载',
    table: {
      slots: {
        default: (data: any) => {
          const mounts =  data.row.mounts ||[]
          return mounts.map(item=>{return mountTypeToLabel.value[item.type]}).join('、')
        }
      }
    },
    search: {
      hidden: true
    },
  },
  {
    field: 'authenticationStatus',
    label: '实名认证状态',
    table: {
      slots: {
        default: (data: any) => {
          return droneAuthStatusToLabel.value[data.row.authenticationStatus]
        }
      }
    },
    search: {
      hidden: true
    },
  },
  {
    field: 'createUserName',
    label: '创建人',
    table: {
      slots: {
        default: (data: any) => {
          return data.row.createUserName
        }
      }
    },
    search: {
      hidden: true
    },
  },
  {
    field: 'action',
    width: '280px',
    label: '操作',
    search: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          return h(tabButton, {
            actions: [
              {
                type: 'button',
                tip: '详情',
                props: {
                  type: 'success',
                  onClick: () => linkTo(data.row, 'detail')
                }
              },
              {
                type: 'button',
                tip: '编辑',
                props: {
                  type: 'primary',
                  onClick: () => linkTo(data.row, 'edit')
                }
              },
              {
                type: 'button',
                tip: '删除',
                props: {
                  type: 'danger',
                  onClick: () => DT.delData(data.row)
                }
              }
            ]
          })
        }
      }
    }
  }
])




